<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\User;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session; 

use App\Commands\OperationLogCommand;
use Illuminate\Support\Facades\Input;
use App\Models\AdminRole;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;
use App\Interfaces\SmsService;

class UserController extends Controller
{

    public function user_gate()
    {
        if (!Gate::allows('user','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    public function index(Request $request)
    {
        $this->user_gate();

        $search = ['role_id'=>'all', 'user_name'=>'','phone'=>'','email'=>''];
        $users = User::orderBy('id','desc');


        if ($request->get('user_name')!='' && $request->get('user_name')!='all'){
            $users = $users->where('user_name', 'like', '%'.trim($request->get('user_name')).'%');
            $search['user_name'] = $request->user_name;
        }//end if()

        if ($request->get('phone')!='' && $request->get('phone')!='all'){
            $users = $users->where('phone', 'like', '%'.trim($request->get('phone')).'%');
            $search['phone'] = $request->phone;
        }

        if ($request->get('email')!='' && $request->get('email')!='all'){
            $users = $users->where('email', 'like', '%'.trim($request->get('email')).'%');
            $search['email'] = $request->email;
        }

        $roleArr = ['all'=>'所有用户',-1=>'后台用户',0=>'前台用户'];
        $admin_roles = AdminRole::all();
        foreach ($admin_roles as $admin_role) {
            $role_id = $admin_role->id;
            $role_name = $admin_role->role_name;
            $roleArr[$role_id] = $role_name;

        }//end foreach()

        if(empty($request->role_id) && $request->role_id != 0){

            $role_id = -1;
            $users = $users->where('role_id', '<>', 0);
            $search['role_id'] = $role_id;
            $role_name = '后台用户';
            $roleArr[$role_id] = $role_name;
        }elseif(empty($request->role_id) && $request->role_id === '0'){

            $users = $users->where('role_id', $request->role_id);
            $search['role_id'] = $request->role_id;
        }elseif(!empty($request->role_id) && $request->role_id!='all'){

            if($request->role_id == -1){

                $users = $users->where('role_id', '<>', 0);
                $search['role_id'] = $request->role_id;
                $role_name = '后台用户';
                $roleArr[$request->role_id] = $role_name;
            }else{
                $users = $users->where('role_id', $request->get('role_id'));
                $search['role_id'] = $request->role_id;

            }//end if()
        }//end if()


        $users = $users->paginate(10);

        return view('admin.user.index',compact('users', 'roleArr', 'search'));
    }//end func index()


    public function create()
    {
        //
    }


    public function store(Request $request)
    {
        //
    }


    // public function show($id)
    // {
    //     dd('show');
    //     //
    // }


    public function edit($id)
    {
        // $users = User::find($id);
        // return view('admin.user.edit',compact('users'));
        //dd("dga");
    }


    public function update(Request $request, $id)
    {
        //dd("daga");
        // $users = User::find($id);

        // $request->request->remove('user_name');

        // $is_disabled = (int)$request->get('is_disabled');
        

        // $users->update($request->all());

        // return Redirect('admin/user');

    }

   
    public function destroy($id)
    {
        
        // $users = User::find($id);
        // $users->delete();

        // return Redirect('admin/user');
    }

    public function toggleDisabled(){

        $this->user_gate();

        $id = Input::get("id");
        $is_disabled = User::where("id",$id)->pluck("is_disabled");        
        $data = array();

        if($is_disabled==0){
            $data['is_disabled'] = 1;
            $data['id'] = $id;
            $res = User::where("id",$id)->update(['is_disabled'=>1]);
//            $this->dispatch(
//                new OperationLogCommand('封禁了用户[user_id:'.$id.']')
//            );
        }else{
            $data['is_disabled'] = 0;
            $data['id'] = $id;
            $res = User::where("id",$id)->update(['is_disabled'=>0]);
//            $this->dispatch(
//                new OperationLogCommand('解封了用户[user_id:'.$id.']')
//            );
        }
        if($res != false){
            $data['info'] = true;

        }else{
            $data['info'] = false;
        }
        return $data;

    }


    /**
     * 添加商品到推荐位置
     *
     *
     * @date 2015/12/19
     * @author Leen
     */
    public function addUserToRole($role_id, Request $request)
    {
        $this->user_gate();

        $return = ['status'=>1, 'message'=>'', 'data'=>''];
        $failUserArr = [];      //添加失败的用户数组
        $userArr = $request->get('userArr');

        $role = AdminRole::findOrNew($role_id);
        if(empty($role)){
            $return['status'] = 0;
            $return['message'] = '要添加到的用户组不存在';

            return Response()->json($return);
        }//end if()

        foreach ($userArr as $key => $value) {
            $user_id = $value;
            $k = 0;

            $user = User::findOrNew($user_id);
            if(empty($user)){
                $failUserArr[$k] = $user_id;
                $return['status'] = 0;
                $return['message'] = '要添加到的用户不存在';
                $return['data'] = $failUserArr;
                $k++;
            }//end if()

            User::find($user_id)->update(['role_id'=>$role_id]);
//            $this->dispatch(
//                new OperationLogCommand('修改了用户角色[user_id:'.$user_id.', role_id'.$role_id.']')
//            );

        }//end foreach()

        return Response()->json($return);

    }//end func addUserToRole()


    /**
     * 获取用户信息
     * @return [type] [description]
     */
    public function getUser(Request $request)
    {

        $search = ['role_id'=>'all', 'user_name'=>''];

        $users = User::orderBy('id','desc');


        if ($request->get('user_name')!='' && $request->get('user_name')!='all'){

            $users = $users->where('user_name', 'like', '%'.$request->get('user_name').'%');

            $search['user_name'] = $request->user_name;
        }


        $roleArr = ['all'=>'所有用户',-1=>'后台用户',0=>'前台用户'];

        $admin_roles = AdminRole::all();

        foreach ($admin_roles as $admin_role) {

            $role_id = $admin_role->id;

            $role_name = $admin_role->role_name;

            $roleArr[$role_id] = $role_name;
        }

        if(empty($request->role_id) && $request->role_id != 0){

            $role_id = -1;

            $users = $users->where('role_id', '<>', 0);

            $search['role_id'] = $role_id;

            $role_name = '后台用户';

            $roleArr[$role_id] = $role_name;

        }elseif(empty($request->role_id) && $request->role_id === '0'){

            $users = $users->where('role_id', $request->role_id);

            $search['role_id'] = $request->role_id;

        }elseif(!empty($request->role_id) && $request->role_id!='all'){

            if($request->role_id == -1){

                $users = $users->where('role_id', '<>', 0);

                $search['role_id'] = $request->role_id;

                $role_name = '后台用户';

                $roleArr[$request->role_id] = $role_name;

            }else{

                $users = $users->where('role_id', $request->get('role_id'));

                $search['role_id'] = $request->role_id;
            }
        }

        $users = $users->paginate(10);

        return $users;
    }


    //根据筛选条件获取用户的数量
    public function getUserNumByCondition(Request $request, SmsService $smsService){

        $return= ['status' => 1, 'message' => '', 'data' => ''];

        $user_condition = empty($request->user_condition) ? 0 : $request->user_condition;

        $users = $smsService->getPhoneByObject($user_condition);

        $return['data'] = count($users);

        return $return;
    }

}
